package com.example.myapplication.ui.account

import android.content.Intent
import android.util.Log
import android.view.Gravity
import android.view.View
import android.widget.AdapterView
import com.example.myapplication.FileActivity
import com.example.myapplication.R
import com.example.myapplication.common.AbstractFragment
import com.example.myapplication.ui.download.FileDownloadFragment
import com.example.myapplication.ui.upload.FileUploadFragment
import com.mylhyl.circledialog.CircleDialog
import com.mylhyl.circledialog.view.listener.OnLvItemClickListener
import com.saop.annotation.AopLog

/**
 * 文件管理Fragment
 */
class FileFragment : AbstractFragment() {

    private val fileDownload = FileDownloadFragment()
    private val fileUpload = FileUploadFragment()

    override fun getContentLayoutId(): Int {
        return R.layout.fragment_file
    }

    override fun initView(contentView: View) {
        super.initView(contentView)
        contentView.findViewById<View>(R.id.bt_file_download).setOnClickListener {
            loadFragment( R.id.fragment_layout, fileDownload, false)
        }
        contentView.findViewById<View>(R.id.bt_file_upload).setOnClickListener {
            loadFragment( R.id.fragment_layout, fileUpload, false)
        }
        contentView.findViewById<View>(R.id.back_text).setOnClickListener {
            backFragment()
        }
        contentView.findViewById<View>(R.id.more).setOnClickListener {
            CircleDialog.Builder().also {
                it.setGravity(Gravity.BOTTOM)
                it.setYoff(50)
                it.setRadius(10)
                it.setItems(mutableListOf<String>().apply {
                    this.add("打开")
                    this.add("预设")
                    this.add("清空进行")
                    this.add("刷新失败")
                    this.add("清空失败")
                }, object : OnLvItemClickListener {
                    override fun onItemClick(
                        parent: AdapterView<*>?,
                        view: View?,
                        position: Int,
                        id: Long
                    ): Boolean {
                        val item = parent?.adapter?.getItem(position) ?: return true
                        childFragmentManager.fragments.forEach {
                            if (it.isVisible && it is AbstractFragment) {
                                return it.onMoreItemSelected(item as String)
                            }
                        }
                        return true
                    }
                })
                it.configItems {
                    it.textColor = requireContext().getColor(R.color.item_color)
                    it.textSize = 18
                }
                it.setNegative("取消") { Log.e("FileActivity", "取消") }
                it.configNegative { params ->
                    params?.textColor = requireContext().getColor(R.color.button_color)
                    params?.textSize = 18
                }
            }.show(childFragmentManager)
        }
    }

    override fun initData() {
        super.initData()
        loadFragment( R.id.fragment_layout , fileDownload, false)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        childFragmentManager.fragments.forEach {
            it.onActivityResult(requestCode, resultCode, data)
        }
        super.onActivityResult(requestCode, resultCode, data)
    }
}